package com.ysd.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.service.PermissionService;


@RestController
public class PermissionController {

	@Autowired
	private PermissionService ps;
	
	private String test333=null;
	//查询所有模块信息
	@ResponseBody
	@RequestMapping("/findPermissions")
	public Object findPermissions(Integer id) {
		if (test333!=null) {
			ps.delR_P(id, test333);
		}
		return ps.getAllPermissions(id);
	}
	//修改树形结构中的父子节点
	//实现添加和取消权限
	@ResponseBody
	@RequestMapping("/updatePer")
	public Object updatePer(HttpServletRequest request) {
		Object rid = request.getParameter("da");
		String pids = request.getParameter("xiao");
		String pidsqs=ps.trimFirstAndLastChar(pids, "[");
		String pidsqw=ps.trimFirstAndLastChar(pidsqs, "]");
		List<String> pidsqsw = java.util.Arrays.asList(pidsqw.split(","));
		List<String> pis=new ArrayList<String>();
		for (int i = 0; i < pidsqsw.size(); i++) {
			String ccc=ps.trimFirstAndLastChar(pidsqsw.get(i),"\"" );
			pis.add(ccc);
		}
		List<Object> checkedids=ps.findPresByRid(rid);
		System.out.println("dd=="+checkedids);
		for (int i = 0; i < pis.size(); i++) {
			String a = pis.get(i);
			if (ps.findPreByR_P(rid,a).isEmpty()) {
				try {
					ps.delR_P(rid,a);
					ps.addR_P(rid,a);
				} catch (Exception e) {
					// TODO: handle exception
				}
				
			}else if (checkedids.contains(a)) {
					//ps.delR_P(rid, a);
					test333=a;
					System.out.println("aaa==="+a);
			}
		}
		return "a";
	}
}
